﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ProtoBuf;
namespace Sharp.Interprocess.MemoryManager
{
    [ProtoContract]
    public class AllocationStateData
    {
        #region Public Properties
        
        [ProtoMember(1)]
        public Dictionary<int, int> AvailableBlockCountBySize { get; private set; }

        [ProtoMember(2)]
        public HashSet<long> AllocatedSegmentOffsets { get; private set; }

        /// <summary>
        /// how much storage is available
        /// </summary>
        [ProtoMember(3, DataFormat = DataFormat.FixedSize)]
        public long TotalStorageSize { get; set; }

        /// <summary>
        /// how much storage is used
        /// </summary>
        [ProtoMember(4, DataFormat = DataFormat.FixedSize)]
        public long UsedStorageSize { get; set; }

        [ProtoMember(5)]
        public HashSet<SegmentHandle> AllocatedSegmentHandles { get; private set; }

        #endregion

        #region Constructor(s)

        private AllocationStateData()
        {
            AllocatedSegmentHandles = new HashSet<SegmentHandle>();
            AllocatedSegmentOffsets = new HashSet<long>();
        }

        /// <summary>
        /// default Ctor
        /// </summary>
        /// <param name="totalStorageSize"></param>
        internal AllocationStateData(int totalStorageSize)
        {            
            var totalStorageSizePowerOf2 = AllocatorUtil.FindClosestSegmentSize(totalStorageSize);
            AvailableBlockCountBySize = new Dictionary<int, int>();
            AllocatedSegmentOffsets = new HashSet<long>();
            AvailableBlockCountBySize.Add(totalStorageSizePowerOf2, 1);
            
            AllocatedSegmentHandles = new HashSet<SegmentHandle>();
            TotalStorageSize = totalStorageSizePowerOf2;
            UsedStorageSize = 0;
        }

        #endregion

    }
}
